<template>
    <a-form-model ref="form"  :model="formData" :rules="rules" :label-col="labelCol" :wrapper-col="wrapperCol">
        <a-row>
            <a-col :span="12">
                <a-form-model-item  :label="i18n('edit1.name','名称')" prop = "name">
                    <a-input :placeholder="i18n('edit1.name','请输入名称','input')" v-model="formData.name"/>
                </a-form-model-item>
            </a-col>
            <a-col :span="12">
                <a-form-model-item  :label="i18n('edit1.key','别名')" prop = "key" class="x-a-form-model-error">
                    <a-input :placeholder="i18n('edit1.key','请输入别名','input')" v-model="formData.key"/>
                </a-form-model-item>
            </a-col>
        </a-row>
        <a-row>
            <a-col :span="12">
                <a-form-model-item  :label="i18n('edit1.treeId','分类')" prop = "treeId">
                    <rx-category-select v-if="isShowManageTree" :isAdmin="isManageToTree" cat-key="FORM" :isGrant="true" v-model="formData.treeId"></rx-category-select>
                </a-form-model-item>
            </a-col>
            <a-col :span="12">
                <a-form-model-item  :label="i18n('form.source','数据来源')" prop = "source">
                    <a-radio-group :options="businessSourceList" v-model="formData.source" />
                </a-form-model-item>
            </a-col>
        </a-row>
        <a-row v-show="formData.source=='businessModel'">
            <a-col :span="24">
                <a-form-model-item  :label="i18n('edit1.businessModel','业务模型')" :label-col="labelCol1" :wrapper-col="wrapperCol1">
                    <rx-input-button @click="onSelectBoDef" @change="boDefChange" v-model="settingJson.boDef" />
                </a-form-model-item>
            </a-col>
        </a-row>
        <a-row>
            <a-col :span="24">
                <a-form-model-item  :label="i18n('edit1.desc','描述')" prop = "desc" :label-col="labelCol1" :wrapper-col="wrapperCol1">
                    <a-textarea :placeholder="i18n('edit1.desc','请输入描述','input')" :rows="4" v-model="formData.desc"/>
                </a-form-model-item>
            </a-col>
        </a-row>
    </a-form-model>
</template>

<script>
    import codegenBusinessSource from "../codegenBusinessSource";
    import {RxInputButton,Dialog} from "@lowcode/jpaas-common-lib";
    import AppManager from "@/views/modules/share/AppManager";
    export default {
        name: "FormCodegenBusinessEdit1",
        props:["formData"],
        mixins:[codegenBusinessSource,AppManager],
        components:{
            RxInputButton
        },
        data() {
            return {
                labelCol: { span:6 },
                wrapperCol: { span:18 },
                labelCol1: { span:3 },
                wrapperCol1: { span:19 },
                settingJson:{
                    boDef:"",
                    bodefId: "",
                    bodefName: "",
                    //临时数据
                    tmpVars:{}
                },
                rules: {
                    name: [{required: true, message: this.i18n('edit1.name','请输入名称','input'), trigger: 'change'}],
                    key: [
                        {required: true, message: this.i18n('edit1.key','请输入别名','input'), trigger: 'change'},
                        {pattern:/^[a-zA-Z][a-zA-Z0-9_-]*$/,message:this.i18n2('form.str','不能以数字开头,不能包含【_-】以外的特殊字符！','input')}
                    ],
                    treeId: [{required: true, message: this.i18n('edit1.treeId','请选择分类','select'), trigger: 'change'}],
                    source: [{required: true, message: this.i18n('form.source','请选择数据来源','select'), trigger: 'change'}]
                }
            }
        },
        created() {
            this.isUserManager(this.$route.query.appId);
            this.init();
            this.initBusinessSourceList();
        },
        methods:{
            i18n(name,text,key){
                return this.$ti18(name,text,"formCodegenBusiness",key);
            },
            i18n2(name,text,key){
                return this.$ti18(name,text,"formBoEntity",key);
            },
            init(){
                let settingJson=this.formData.settingJson;
                if(settingJson){
                    this.settingJson=JSON.parse(settingJson);
                    this.settingJson.tmpVars={};
                    if(this.formData.source=="businessModel") {
                        this.boDefChange(this.settingJson.boDef);
                    }
                }
            },
            handleSubmit(self, this_,callback) {
                this_.$refs.form.validate(valid => {
                    if(valid){
                        if(this_.formData.source=='businessModel' && !this_.settingJson.bodefId){
                            this_.$message.warning(this_.i18n('edit1.businessModel','请选择业务模型!','select'));
                            return callback(true);
                        }
                        this_.formData.settingJson = JSON.stringify(this_.settingJson);
                        this_.$emit("changeForm", this_.formData);
                    }
                    callback(!valid);
                })
            },
            boDefChange(value){
                if(value){
                    let json=JSON.parse(value);
                    this.settingJson.bodefId = json.value;
                    this.settingJson.bodefName = json.text;
                    this.loadMainEntityByBoDefId();
                }else{
                    this.settingJson.bodefId="";
                    this.settingJson.bodefName="";
                    this.settingJson.tmpVars={};
                }
            },
            /*
             *选择业务模型
             */
            onSelectBoDef(vm) {
                let self_ = this;
                Dialog.openBoDefDialog({
                    curVm: this, widthHeight: ['900px', '600px']
                }, function (data) {
                    if(!data || data.id==self_.settingJson.bodefId){
                        return;
                    }
                    vm.setVal(data.id, data.name);
                })
            },
            loadMainEntityByBoDefId(){
                if(!this.settingJson.bodefId){
                    return;
                }
                let self_=this;
                let params={
                    source:this.formData.source,
                    boDefId:this.settingJson.bodefId
                }
                this.reloadBusinessColumns(params,function(res){
                    let data = res.data;
                    self_.settingJson.tmpVars["mainEntity"]=data.boEnt;
                })
            }
        },
        watch:{
            formData:{
                handler:function(val){
                    this.init();
                },
                deep:true
            }
        }
    }
</script>

<style scoped>

</style>